package com.handu.luna.core.domain.advertisement;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.Transient;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.format.annotation.DateTimeFormat;

import com.handu.luna.core.domain.BasePersistentDomain;
import com.handu.luna.core.domain.advertisement.AdPositionId.BelongPage;
import com.handu.luna.core.domain.search.AdvertiseSearch;

@Entity
public class AdvertiseList extends BasePersistentDomain<AdvertiseListRepository>{
	
	@NotNull(message="广告名称不能为空")
	@Size(min=1,max=100, message="广告名称长度超过限制！")
	private String advertiseName;  //广告名称
	
	private MediaType mediaType;  // 媒介类型   要定义为枚举类型的
	
	public enum MediaType{ 
		IMAGE("图片");		
	    public String description;	    
	    MediaType(final String description) {
	        this.description = description;
	    }
	}
	
	
	
	private long adPositionId;   //广告位ID
	@Min(0) 
	@Max(value=254,message="最大不能超过255")
	private int sortOrder;  //排序
	
	@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date startDate;   //开始时间	
	
	@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	private Date endDate;    //结束时间	
	
	private String advertiseLink;  //广告链接
	
	private String advertiseImageUrl;   //选择上传广告图片或 填写广告链接	
	private boolean  displayed;   //isShow;   //是否显示
	
	@Lob
	private String description;  //网页描述
	private int clickCount;  //点击次数
	
	
	public String getAdvertiseName() {
		return advertiseName;
	}

	public void setAdvertiseName(String advertiseName) {
		this.advertiseName = advertiseName;
	}



	public MediaType getMediaType() {
		return mediaType;
	}

	public void setMediaType(MediaType mediaType) {
		this.mediaType = mediaType;
	}

	public long getAdPositionId() {
		return adPositionId;
	}

	public void setAdPositionId(long adPositionId) {
		this.adPositionId = adPositionId;
	}

	public int getSortOrder() {
		return sortOrder;
	}

	public void setSortOrder(int sortOrder) {
		this.sortOrder = sortOrder;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public String getAdvertiseLink() {
		return advertiseLink;
	}

	public void setAdvertiseLink(String advertiseLink) {
		this.advertiseLink = advertiseLink;
	}

	public String getAdvertiseImageUrl() {
		return advertiseImageUrl;
	}

	public void setAdvertiseImageUrl(String advertiseImageUrl) {
		this.advertiseImageUrl = advertiseImageUrl;
	}

	public boolean isDisplayed() {
		return displayed;
	}

	public void setDisplayed(boolean displayed) {
		this.displayed = displayed;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public int getClickCount() {
		return clickCount;
	}

	public void setClickCount(int clickCount) {
		this.clickCount = clickCount;
	}


	

	
	@Transient
	private AdPositionId adPositionName;
	
	public AdPositionId getAdPositionName() {
		return adPositionName;
	}

	public void setAdPositionName(AdPositionId adPositionName) {
		this.adPositionName = adPositionName;
	}

	/**
	 * 多表查询的   初始化信息
	 */
	public void init(){
		//articleCategory = context.getArticleCategoryRepository().findOne(catId);
		adPositionName = context.getAdPositionIdRepository().findOne(adPositionId);
	}
	
	public Page<AdvertiseList> findAll(Pageable pageable){
		Page<AdvertiseList> advertiseList = context.getAdvertiseListRepository().findAll(pageable);
		for(AdvertiseList List : advertiseList){
			List.init();
		}
		return advertiseList;
	}
	
	/**
	 * 点击广告位，跳到相应的广告列表
	 * @param pageable
	 * @return
	 */
	public Page<AdvertiseList> findAll(long id,Pageable pageable){
		Page<AdvertiseList> advertiseList = context.getAdvertiseListRepository().findAll(id,pageable);
		for(AdvertiseList List : advertiseList){
			List.init();
		}
		return advertiseList;
	}
	
	
	public Page<AdvertiseList> findAll(AdvertiseSearch advertiseSearch,Pageable pageable){
		Page<AdvertiseList> advertiseList = context.getAdvertiseListRepository().findAll(AdvertiseList.selectCondition(advertiseSearch), pageable);
		for(AdvertiseList List : advertiseList){
			List.init();
		}
		return advertiseList;
	}
	
	
	
	public AdvertiseList(){}
	
	public AdvertiseList(String advertiseName,MediaType mediaType, long adPositionId,int sortOrder ,Date startDate, Date endDate,String advertiseLink,String advertiseImageUrl,boolean displayed,String description,int clickCount){
		super();
		this.advertiseName = advertiseName;
		this.mediaType = mediaType;
		this.adPositionId = adPositionId;
		this.sortOrder = sortOrder;
		this.startDate = startDate;
		this.endDate = endDate;
		this.advertiseLink = advertiseLink;
		this.advertiseImageUrl = advertiseImageUrl;
		this.displayed = displayed;
		this.description = description;
		this.clickCount = clickCount;

	}
	
	public static Specification<AdvertiseList> selectCondition(AdvertiseSearch advertiseSearch) {
		return new Specification<AdvertiseList>() {
			@Override
			public Predicate toPredicate(Root<AdvertiseList> root,
					CriteriaQuery<?> query, CriteriaBuilder builder) {

				Predicate predicate = builder.conjunction();
				List<Expression<Boolean>> expressions = predicate.getExpressions();
				if(advertiseSearch!=null){
					if (StringUtils.isNotBlank(advertiseSearch.getAdName())) {
					   expressions.add(builder.like(root.<String> get("advertiseName"), "%"+advertiseSearch.getAdName() + "%")); // 广告名称
					}
					
				   if (NumberUtils.isDigits(advertiseSearch.getAdPositionNameById())) {
					  expressions.add(builder.equal(root.<Long> get("adPositionId"),advertiseSearch.getAdPositionNameById())); // 分类id
				    }
			    }
				return query.where(predicate).getRestriction();
			}
		};
	}
	

/**
 * 首页   广告管理      lxx
 * @param belongPage
 * @return
 */	
	 public  HashMap<String,List<AdvertiseList>>  getAdListsInfo(BelongPage belongPage){	
		 String beforeUrl = "http://192.168.1.182:8999";  //"http://192.168.1.182:8999";//"http://localhost:8999";
		 List<AdPositionId> adPositions = context.getAdPositionIdRepository().findAllAdPositions(belongPage);
		 HashMap<String, List<AdvertiseList>> map = new HashMap<String , List<AdvertiseList>>(); 
		 for(AdPositionId adPositionId : adPositions){
			 List<AdvertiseList> ads = context.getAdvertiseListRepository().findAllFront(adPositionId.getId());
			 List<AdvertiseList> finalAds = new ArrayList<AdvertiseList>();
			 for(AdvertiseList ad : ads){				 
				 if(!ad.getAdvertiseImageUrl().startsWith("http://", 0)){
					 ad.setAdvertiseImageUrl(beforeUrl + ad.getAdvertiseImageUrl());
				 }
				 finalAds.add(ad);
			 }
	    		map.put(adPositionId.getAdPositionSymbol(), finalAds);//context.getAdvertiseListRepository().findAllFront(adPositionId.getId())
	     }
		 return map;		 
	 }
	
	
	
}
